package per.ethan.cloud.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.HttpStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import per.ethan.cloud.common.domain.ErrorCodeVO;
import per.ethan.cloud.common.exception.ServiceException;
import per.ethan.cloud.common.service.RedisTemplateService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author Ethan
 * @date 2020/04/03 10:06
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class RedisTemplateServiceImpl implements RedisTemplateService {

    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean exists(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            log.error("RedisTemplateService method hasKey is error, key: {}", key);
            return false;
        }
    }

    @Override
    public boolean expire(String key, long expireTime) {
        try {
            if (expireTime > 0) {
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method expire is error, key: {}", key);
            return false;
        }
    }

    @Override
    public Long getExpire(String key) {
        try {
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("RedisTemplateService method getExpire is error, key: {}", key);
            return null;
        }
    }

    @Override
    public boolean set(String key, String value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method set is error, key: {}, value: {}, {}", key, value, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean set(String key, String value, long expireTime) {
        try {
            redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method set is error, key: {}, value: {}, {}", key, value, e.getMessage());
            return false;
        }
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean delete(String... key) {
        try {
            if (ArrayUtil.isNotEmpty(key)) {
                if (1 == ArrayUtil.length(key)) {
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete(CollUtil.toList(key));
                }
            }

            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method set is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public Long increment(String key, long incrNum) {
        if (incrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "增长因子必须大于0"));
        }

        try {
            return redisTemplate.opsForValue().increment(key, incrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method increment is error, key: {}, incrNum: {}, {}", key, incrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Double increment(String key, Double incrNum) {
        if (incrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "增长数据必须大于0"));
        }

        try {
            return redisTemplate.opsForValue().increment(key, incrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method increment is error, key: {}, incrNum: {}, {}", key, incrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Long decrement(String key, long decrNum) {
        if (decrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "递减因子必须大于0"));
        }

        try {
            return redisTemplate.opsForValue().increment(key, -decrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method decrement is error, key: {}, decrNum: {}, {}", key, decrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Double decrement(String key, Double decrNum) {
        if (decrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "递减数据必须大于0"));
        }

        try {
            return redisTemplate.opsForValue().increment(key, -decrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method decrement is error, key: {}, decrNum: {}, {}", key, decrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public boolean hSet(String key, String item, String value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean hSet(String key, String item, Object value, long expireTime) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            expire(key, expireTime);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public Object hGet(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    @Override
    public boolean hDelete(String key, Object... item) {
        try {
            redisTemplate.opsForHash().delete(key, item);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hDelete is error, key: {}, item: {}, {}", key, item, e.getMessage());
            return false;
        }
    }

    @Override
    public Long hIncrement(String key, String item, long incrNum) {
        if (incrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "增长数据必须大于0"));
        }
        try {
            return redisTemplate.opsForHash().increment(key, item, incrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method hIncrement is error, key: {}, incrNum: {}, {}", key, incrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Double hIncrement(String key, String item, Double incrNum) {
        if (incrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "递增因子必须大于0"));
        }
        try {
            return redisTemplate.opsForHash().increment(key, item, incrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method hIncrement is error, key: {}, incrNum: {}, {}", key, incrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Long hDecrement(String key, String item, long decrNum) {
        if (decrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "递减因子必须大于0"));
        }

        try {
            return redisTemplate.opsForHash().increment(key, item, -decrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method hDecrement is error, key: {}, decrNum: {}, {}", key, decrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public Double hDecrement(String key, String item, Double decrNum) {
        if (decrNum < 0) {
            throw new ServiceException(new ErrorCodeVO(HttpStatus.HTTP_INTERNAL_ERROR, "递减因子必须大于0"));
        }

        try {
            return redisTemplate.opsForHash().increment(key, item, -decrNum);
        } catch (Exception e) {
            log.error("RedisTemplateService method hDecrement is error, key: {}, decrNum: {}, {}", key, decrNum, e.getMessage());
            return null;
        }
    }

    @Override
    public boolean hashKeyExists(String key, String item) {
        try {
            return redisTemplate.opsForHash().hasKey(key, item);
        } catch (Exception e) {
            log.error("RedisTemplateService method hashKeyExists is error, key: {}, item：{}, {}", key, item, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean hmSet(String key, Object hashKey, Object value) {
        try {
            redisTemplate.opsForHash().put(key, hashKey, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hmSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean hmSet(String key, Object hashKey, Object value, long expireTime) {
        try {
            redisTemplate.opsForHash().put(key, hashKey, value);
            expire(key, expireTime);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hmSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean hmSet(String key, Map<String, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hmSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean hmSet(String key, Map<String, Object> map, long expireTime) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            expire(key, expireTime);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method hmSet is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public Map<Object, Object> hmGet(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            log.error("RedisTemplateService method hmSet is error, key: {}, {}", key, e.getMessage());
            return Collections.emptyMap();
        }
    }

    @Override
    public Long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            log.error("RedisTemplateService method sSet is error, key: {}, {}", key, e.getMessage());
            return null;
        }
    }

    @Override
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            log.error("RedisTemplateService method sGet is error, key: {}, {}", key, e.getMessage());
            return Collections.emptySet();
        }
    }

    @Override
    public boolean setExists(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            log.error("RedisTemplateService method setExists is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public Long sSet(String key, long expireTime, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            expire(key, expireTime);
            return count;
        } catch (Exception e) {
            log.error("RedisTemplateService method sSetExpire is error, key: {}, {}", key, e.getMessage());
            return null;
        }
    }

    @Override
    public long sSetGetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            log.error("RedisTemplateService method sSetGetSize is error, key: {}, {}", key, e.getMessage());
            return 0;
        }
    }

    @Override
    public boolean setDelete(String key, Object... values) {
        try {
            redisTemplate.opsForSet().remove(key, values);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method setDelete is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean lPush(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method lPush is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean lPush(String key, Object value, long expireTime) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            expire(key, expireTime);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method lPush is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean lPush(String key, List<Object> list) {
        try {
            redisTemplate.opsForList().rightPushAll(key, list);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method lPush is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean lPush(String key, List<Object> list, long expireTime) {
        try {
            redisTemplate.opsForList().rightPushAll(key, list);
            expire(key, expireTime);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method lPush is error, key: {}, {}", key, e.getMessage());
            return false;
        }
    }

    @Override
    public List<Object> lRange(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            log.error("RedisTemplateService method lRange is error, key: {}, start: {}, end: {}, {}", key, start, end, e.getMessage());
            return Collections.emptyList();
        }
    }

    @Override
    public long getListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            log.error("RedisTemplateService method getListSize is error, key: {}, {}", key, e.getMessage());
            return 0;
        }
    }

    @Override
    public Object getListIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            log.error("RedisTemplateService method getListSize is error, key: {}, index: {}, {}", key, index, e.getMessage());
            return null;
        }
    }

    @Override
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method getListSize is error, key: {}, index: {}, {}", key, index, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean lDelete(String key, long count, Object value) {
        try {
            redisTemplate.opsForList().remove(key, count, value);
            return true;
        } catch (Exception e) {
            log.error("RedisTemplateService method getListSize is error, key: {}, count: {}, {}", key, count, e.getMessage());
            return false;
        }
    }

}
